package com.vpen.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vpen.blog.common.ReturnResult;
import com.vpen.blog.config.security.JwtTokenUtils;
import com.vpen.blog.entity.Blog;
import com.vpen.blog.entity.User;
import com.vpen.blog.mapper.BlogMapper;
import com.vpen.blog.service.BlogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vpen.blog.service.UserService;
import com.vpen.blog.utils.BlogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Vpen
 * @since 2021-07-07
 */
@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements BlogService {
    @Autowired
    private BlogUtils blogUtils;

    @Autowired
    private BlogMapper blogMapper;


    // 配置文件里有写
    @Value("${jwt.tokenHead}")
    private String tokenHead;
    // 前端存在Session中的token的key
    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    /**
     * 作者:Vpen
     * 描述: 添加文章
     * */
    public ReturnResult addArticle(Map<String,Object> article){
        Blog blog = blogUtils.encodeBlog(article);// 封装数据实体类
        blogMapper.insert(blog);// 添加到数据库
        return ReturnResult.getInstance().success("文章发表成功",null);
    }
    /**
     * 作者:Vpen
     * 描述:删除
     * */
    @Override
    public Boolean deleteArticle(Long id) {
        int i = blogMapper.deleteById(id);
        return i > 0;
    }

    /**
     * 作者:Vpen
     * 描述: 获取文章
     * */
    @Override
    public List<Blog> getArticles() {
        return blogMapper.selectList(new QueryWrapper<Blog>());
    }
}
